package com.iamgod.savemoney.plan;

import java.util.Calendar;

import android.app.TimePickerDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TimePicker;

import com.iamgod.savemoney.R;
import com.iamgod.savemoney.base.BaseActivity;
import com.iamgod.savemoney.bean.PlanConfigBean;
import com.iamgod.savemoney.dao.PlanConfigDao;
import com.iamgod.savemoney.sys.Config;

/**
 *设置计划
 * @author llj
 *
 */
public class SetPlanAct extends BaseActivity {
	
	TextView nav_tit,tv_yi,tv_shi,tv_zhu,tv_xing,tv_wan,tv_qita;//顶部导航标题
	EditText et_plan_name,et_plan_pay,et_time;
	Button bt_settime,bt_submit,bt_back;
	TimePicker tp;
	Calendar 	c;
	ListView planList;//数据列表
	String planType;
	PlanConfigBean bean;
	PlanConfigDao dao;
	String consumeType;//消费类别
	static String TAG = "SetPlanAct";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.set_plan_act);
		planType = this.getIntent().getExtras().getString("planType");
		c=Calendar.getInstance();
		Log.i(TAG, "onCreate--"+ planType);
		//initNav(this);
		initUI();
		
		
	}
	
	
	void initUI(){
		nav_tit = (TextView) this.findViewById(R.id.tv_tit);
		planList = (ListView) this.findViewById(R.id.lv_list);
		tv_yi = (TextView) this.findViewById(R.id.tv_yi);
		tv_shi = (TextView) this.findViewById(R.id.tv_shi);
		tv_zhu = (TextView) this.findViewById(R.id.tv_zhu);
		tv_xing = (TextView) this.findViewById(R.id.tv_xing);
		tv_wan = (TextView) this.findViewById(R.id.tv_wan);
		tv_qita = (TextView) this.findViewById(R.id.tv_qita);
		
		et_plan_name = (EditText) findViewById(R.id.et_plan_name);
		et_plan_pay = (EditText) findViewById(R.id.et_plan_pay);
		et_time = (EditText) findViewById(R.id.et_plan_time);
		
		bt_settime = (Button) findViewById(R.id.bt_settime);
		bt_submit = (Button) findViewById(R.id.bt_submit);
		bt_back = (Button) findViewById(R.id.bt_back);
		
		tv_yi.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View v) {
				setViewColor(tv_yi.getText().toString());
				consumeType = tv_yi.getText().toString();
				
			}
			
		});
		tv_shi.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View v) {
				setViewColor(tv_shi.getText().toString());
				consumeType = tv_shi.getText().toString();
			}
			
		});
		tv_zhu.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View v) {
				setViewColor(tv_zhu.getText().toString());
				consumeType = tv_zhu.getText().toString();
			}
			
		});
		tv_xing.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View v) {
				setViewColor(tv_xing.getText().toString());
				consumeType = tv_xing.getText().toString();
			}
			
		});
		tv_wan.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View v) {
				setViewColor(tv_wan.getText().toString());
				consumeType = tv_wan.getText().toString();
			}
			
		});
		tv_qita.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View v) {
				setViewColor(tv_qita.getText().toString());
				consumeType = tv_qita.getText().toString();
			}
			
		});
		//设置计划提醒时间
		bt_settime.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View v) {
				int hour = 0;
				int min = 0;
				if(et_time.getText().toString()==null||et_time.getText().toString().equals("")){
					hour = c.get(Calendar.HOUR_OF_DAY);
					min = c.get(Calendar.MINUTE);
				}else{
					String[] timeStr = et_time.getText().toString().split(":");
					hour = Integer.parseInt(timeStr[0]);
					min = Integer.parseInt(timeStr[1]);
				}
				new TimePickerDialog(SetPlanAct.this,
						new TimePickerDialog.OnTimeSetListener()
						{
							public void onTimeSet(TimePicker view, int hourOfDay,int minute)
							{
								//设置时间
								Log.i(TAG, "onTimeSet--hourOfDay:"+hourOfDay+ "--minute:"+minute);
								String hourStr = null;
								if(hourOfDay<10){
									hourStr = "0"+hourOfDay;
								}else{
									hourStr = ""+hourOfDay;
								}
								String minuteStr = null;
								if(minute<10){
									minuteStr = "0"+minute;
								}else{
									minuteStr = ""+minute;
								}
								et_time.setText(hourStr+":"+minuteStr);
							}
						},
						hour, //设置小时
						min, //设置分钟
						true)
				.show();
				}
			
		});
		
		bt_back.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View v) {
				SetPlanAct.this.finish();
			}
			
		});
		
		bt_submit.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View v) {
				
					dao = new PlanConfigDao(SetPlanAct.this, Config.DataBase.DATABASE_NAME);
					bean = new PlanConfigBean();
					bean.setPlanName(et_plan_name.getText().toString());
					bean.setPlanPay(et_plan_pay.getText().toString());
					bean.setPlanTime(et_time.getText().toString());
					bean.setConsumeType(consumeType);
					bean.setPlanType(planType);
					dao.insert(bean);
					dao.close();
					SetPlanAct.this.operDialogAndFinish("操作提示", "信息已保存");
			}
			
		});
		
		setViewColor(tv_shi.getText().toString());
		consumeType = tv_shi.getText().toString();
		//nav_tit.setText("2012年10月计划消费执行");
	}
	
//	void putData(){
//		Log.i(TAG, "putData--"+bean.getPlanName());
//		et_plan_name.setText(bean.getPlanName());
//		et_plan_pay.setText(bean.getPlanPay());
//		et_time.setText(bean.getPlanTime());
//		setViewColor(bean.getConsumeType());
//		consumeType = bean.getConsumeType();
//	}
	
	/**
	 * 对计划类别的UI显示做处理，选定的类别，背景色发生变化
	 * @param str
	 */
	void setViewColor(String str){
		Log.i(TAG, "setViewColor:"+str);
		if(tv_yi.getText().toString().equals(str)){
			tv_yi.setBackgroundResource(R.color.orange);
		}else{
			tv_yi.setBackgroundResource(R.color.navajowhite);
		}
		if(tv_shi.getText().toString().equals(str)){
			tv_shi.setBackgroundResource(R.color.orange);
		}else{
			tv_shi.setBackgroundResource(R.color.navajowhite);
		}
		if(tv_zhu.getText().toString().equals(str)){
			tv_zhu.setBackgroundResource(R.color.orange);
		}else{
			tv_zhu.setBackgroundResource(R.color.navajowhite);
		}
		if(tv_xing.getText().toString().equals(str)){
			tv_xing.setBackgroundResource(R.color.orange);
		}else{
			tv_xing.setBackgroundResource(R.color.navajowhite);
		}
		if(tv_wan.getText().toString().equals(str)){
			tv_wan.setBackgroundResource(R.color.orange);
		}else{
			tv_wan.setBackgroundResource(R.color.navajowhite);
		}
		if(tv_qita.getText().toString().equals(str)){
			tv_qita.setBackgroundResource(R.color.orange);
		}else{
			tv_qita.setBackgroundResource(R.color.navajowhite);
		}
	}
}
